package com.wangbl.infomanager;

import java.util.Arrays;

import org.json.JSONException;
import org.json.JSONObject;

import com.wangbl.infomanager.utils.Logger;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class InfoDetailActivity extends BaseActivity {
	
	protected static final String TAG = "InfoDetailActivity";
	private WebView wv;
	private String url;
	private SharedPreferences push_state;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_info_detail);
		super.onCreate(savedInstanceState);
		Logger.i(TAG, "onCreate");
		sp = getSharedPreferences("push_info", Context.MODE_PRIVATE);
		push_state = getSharedPreferences("push_msg_state", Context.MODE_PRIVATE);
		title_text.setText("详情");
		title_user.setVisibility(View.GONE);
		title_menu.setVisibility(View.GONE);
		title_back.setVisibility(View.VISIBLE);
		url = getIntent().getStringExtra("url");
		if(TextUtils.isEmpty(url)){
			handlePush();
		}
		pd.show();
		pd.setCancelable(true);
		wv = (WebView) findViewById(R.id.login_webview);
		WebSettings setting = wv.getSettings();
		setting.setJavaScriptEnabled(true);
		setting.setBuiltInZoomControls(true);
		setting.setUseWideViewPort(true); 
        setting.setLoadWithOverviewMode(true); 
		wv.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
			//	pd.show();
			}
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Logger.i(TAG, "shouldOverrideUrlLoading:" + url);

//				if (url != null) {
//					int index = url.indexOf("username=");
//					if (index != -1) {
//						String str = url.substring(index);
//						String[] strs = str.split("=");
//						if (strs != null && strs.length > 1) {
//							edit.putString("username", strs[1]);
//							edit.apply();
//							Intent intent = new Intent(LoginActivity.this, InfoMainActivity.class);
//							LoginActivity.this.startActivity(intent);
//							finish();
//						} else {
//							toast("登录失败,请返回重新登录...");
//						}
//					}
//				}
				return false;
			}

			@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				handler.proceed();
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				Logger.i(TAG, "onPageFinished");
				pd.dismiss();
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);
				pd.dismiss();
				toast(description);
			}

			@Override
			public void onReceivedLoginRequest(WebView view, String realm,
					String account, String args) {
				Logger.i(TAG, realm + "   " + account + "  " + args);
				pd.dismiss();
			}

		});
		wv.loadUrl(url);
	}
	
	private void handlePush(){
		Logger.i(TAG, "handlePush");
		Bundle bundle = getIntent().getExtras();
		String push_id = bundle.getString("push_id");
		Logger.i(TAG, "push_id:"+push_id);
		String da = sp.getString(push_id, "");
		String[] datas = da.split("##"); 
		Logger.i(TAG, Arrays.toString(datas));
		if(datas.length != 3){
			toast("数据错误...");
			return;
		}
		String id, type;
		try {
			String data = datas[2];
			JSONObject jsonObj = new JSONObject(data);
			id = jsonObj.getString("fromer");
			type = jsonObj.getString("fromer_icon");
			url = "http://irmsma.ruc.edu.cn/dis.php?";
			if(TextUtils.isEmpty(id)){
				id = "aaa";
			}
			if(TextUtils.isEmpty(type)){
				type = "aaa";
			}
			url = url + "id="+id+"&type="+type;
			if(!push_state.getBoolean(push_id, true)){
				int readCount = push_state.getInt("readCount", 0);
				Editor edit = push_state.edit();
				edit.putInt("readCount", --readCount);
				edit.putBoolean(push_id, true);
				edit.apply();
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	private SharedPreferences sp;
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		wv.destroy();
	}

	@Override
	protected void interrupt() {
		
	}

}
